package com.epoint.mobileoa.ui.notice;

import java.util.HashMap;
import java.util.List;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.model.FeedBackModel;
import com.epoint.mobileoa.service.MainService;

/**
 * @author liyc
 * @time 2012-7-9 下午5:30:11
 * @annotation 
 */
public class FeedBackView extends SuperView {

	private ListView lv;
	private List<FeedBackModel> list;
	private EditText etFeedbackContent;
	private Button btFeedbackContentSend;
	private String infoguid;
	@Override
	public void initControls() {
		etFeedbackContent = (EditText) findViewById(R.id.etFeedContent);
		btFeedbackContentSend = (Button) findViewById(R.id.btFeedContentSend);
		btFeedbackContentSend.setOnClickListener(this);
		//		showTopRightBtn("添加");
		lv = (ListView) findViewById(R.id.lvNotice);
		infoguid = getIntent().getStringExtra("infoguid");
		showRightProgressBar();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("infoguid", infoguid);
		Task task = new GetFeedBackInfoTask(this, param);
		MainService.addTask(task);
	}
	MyAdapter adapter;
	@Override
	public void refresh(Object... param) {
		hideRightProgressBar();
		if(param[0].toString().equals(MainService.NORMAL)){
			String bs = param[1].toString();
			list = GetFeedBackInfoTask.XmlAnalysis(bs);
			adapter= new MyAdapter();
			lv.setAdapter(adapter);
		}
	}

	@Override
	public void onClick(View v) {
		if(v==btFeedbackContentSend)
		{
			if(etFeedbackContent.getText().toString().trim().length()==0)
			{
				Toast.makeText(this, "请添加反馈信息再发送！", Toast.LENGTH_SHORT).show();
			}
			else{
				HashMap<String, Object> params = new HashMap<String, Object>();
				params.put("infoguid", infoguid);
				params.put("feedcontent", etFeedbackContent.getText().toString());
				new SendFeedBackContentTask(this, 1, params, "正在添加..", 2);
				InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(etFeedbackContent.getWindowToken(), 0);
			}
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.feedbacklist;
	}

	@Override
	public String setTitle() {
		return "反馈信息";
	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(FeedBackView.this);

				convertView = mInflater.inflate(R.layout.chuchaidetaillistview, null);

				holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
				holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
				holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
				holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
				holder.tv5 = (TextView) convertView.findViewById(R.id.tv5);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			FeedBackModel f = list.get(position);
			holder.tv1.setVisibility(View.GONE);
			holder.tv4.setVisibility(View.GONE);
			holder.tv5.setVisibility(View.GONE);
			String infoPosterAndTime = "【"+(1+position)+"】 "+f.postdisplayname+" "+f.postdate+"";
			setTextViewFontColor(holder.tv2);
			holder.tv2.setText(infoPosterAndTime);

			holder.tv3.setText(Html.fromHtml("<font color='"+getYGColor()+"'>"+f.content+"</font>"));
			return convertView;
		}
	}

	public class ViewHolder {
		public TextView tv1;
		public TextView tv2;
		public TextView tv3;
		public TextView tv4;
		public TextView tv5;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
		if(TaskId==1)
		{
			String bs = param[0].toString();
			list = GetFeedBackInfoTask.XmlAnalysis(bs);
			adapter.notifyDataSetChanged();
			etFeedbackContent.setText("");
		}
	}

}
